<?php 
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
	if (!isset($_POST['TimeInvoice'])) {$_POST['TimeInvoice'] = date("Y-m-d");}
	MYDCONN();
	$GLOBALS['MYT']=$GLOBALS['MYD'];
	form_pr("InsertInvoice",'','','post');
	mn_field('hidden','ED','CODE','0','','','','');
	mn_field('hidden','ED','Provider','0','','','','');

	$GET = $GLOBALS['MYD']->db_array("SELECT DATE(a.TimeInvoice) AS TimeInvoice,
						 DATE(a.TimePay) AS TimePay,
						 z.name AS Status,
						 b.name AS office,
						 b.ref AS off,
						 a.Ref 
						 FROM ccenter_invoices AS a
						 INNER JOIN main_offices AS b ON b.ref = {$_SESSION[$SESSOF]} 
						 INNER JOIN hcce_status AS z ON z.StatRef = a.Status AND z.Type = 9
						 WHERE a.Type = {$_GET['Tp']}
						 AND  TimeInvoice <= '{$_POST['TimeInvoice']}' 
						 ORDER BY TimeInvoice DESC LIMIT 0,1");
	$Amount = '0,00';
	
	mn_field('hidden','ED','RefInvoice',$GET[1]['Ref'],'','','','');
	mn_field('hidden','ED','Office',$GET[1]['off'],'','','','');

	?>1
<script charset="utf-8" type="text/javascript">
function ArticleAmount(frmObj){
var amnt = (frmObj.ArtQuantity.value*(frmObj.ArtUAmount.value-frmObj.ArtPromo.value))*((frmObj.ArtTaxPercent.value/100)+1);
document.InsertInvoice.ArtAmount.value = amnt.toFixed(2); 
}</script>
<div class="title">Petty Cash - De <?php mn_field('date','ED','TimeInvioce',$GET[1]['TimeInvoice'],'InsertInvoice','8','',''); ?>
      a <?php mn_field('date','ED','DatePay',$GET[1]['TimePay'],'InsertInvoice','8','',''); ?> | Estado: <?php echo $GET[1]['Status'];?>
	  | Loja <input name="text" type="text" disabled value="<?php echo $GET[1]['office'];?>" size="40" /></div>
<table width="100%"  border="0" cellpadding="3" cellspacing="1">
    <tr bgcolor="#D4D4D4">
      <td colspan="4" valign="top">
<div id="invItems" style=" height:450px; overflow:auto">
	<table width="100%"  border="0" cellpadding="2" cellspacing="1" bgcolor="#EAEAEA">
        <tr bgcolor="#F8F8F8">
          <td colspan="4">Descrição</td>
          <td>Natureza</td>
          <td>Fornecedor</td>
          <td>Funcionário</td>
          <td>Quant.</td>
          <td>PreÇo</td>
          <td>Prom.</td>
          <td>IVA</td>
          <td>Total</td>
          <td rowspan="2" align="center">
		  <?php mn_field('button','ED','addArticle','','','','','insert" onclick="
			document.InsertInvoice.action=\'_response/rsproxy.php?pr=cdc_article\';
			subform(\'InsertInvoice\',\'invItems\');'); ?></td>
        </tr>
        <tr bgcolor="#F8F8F8">
          <td colspan="4"><?php mn_field('text','ED','ArtDescription','','255','30','',''); ?></td>
          <td>-</td>
          <td>-</td>
          <td><?php mn_field('select','ED','ArtUser','',"SELECT ref AS ID, NAME FROM main_users WHERE ref_office = {$_SESSION[$SESSOF]} AND Type = 2 ORDER BY name",'','','');?></td>
          <td><?php mn_field('text','ED','ArtQuantity','1','10','2','','" onKeyUp="ArticleAmount(this.form);'); ?></td>
          <td><?php mn_field('text','ED','ArtUAmount','0.00','14','4','','" onKeyUp="ArticleAmount(this.form);'); ?></td>
          <td><?php mn_field('text','ED','ArtPromo','0.00','6','4','','" onKeyUp="ArticleAmount(this.form);'); ?></td>
          <td><?php mn_field('select','ED','ArtTaxPercent','',"SELECT PERCENT AS ID, PERCENT AS NAME FROM ccenter_gov_tax",'','','" onChange="ArticleAmount(this.form);');?></td>
          <td><?php mn_field('text','ED','ArtAmount','0.00','20','4','','" readonly="'); ?></td>
        </tr>
        <?php
	   $List = $GLOBALS['MYD']->db_array("SELECT 
	      			main_users.name AS User,
					main_offices.name AS Office,
	   				a.ArtReference,
	   				a.Ref,
	   				a.ArtReference,
					a.Description,
					a.Quantity,
					a.UAmount,
					a.IvaPercent,
					a.Promo,
					a.Amount,
					a.CODE,
					a.Provider
	  			FROM ccenter_invarticles AS a
				LEFT OUTER JOIN main_users ON main_users.ref = a.User
				LEFT OUTER JOIN main_offices ON main_offices.ref = a.Office
				WHERE RefInvoice = {$GET[1]['Ref']} 
				AND a.Office = {$_SESSION[$SESSOF]}
				AND a.Status != 5");
	for($r = 1; $r <= $List[0]; $r++) {
  echo '<tr bgcolor="#B0C0D0">
        <td colspan="4">'.$List[$r]['Description'].'</td>
        <td>'.$List[$r]['CODE'].'</td>
        <td>'.$List[$r]['Provider'].'</td>
        <td>'.$List[$r]['User'].'</td>
        <td align="right">'.$List[$r]['Quantity'].'</td>
        <td align="right">'.$List[$r]['UAmount'].'</td>
        <td>'.$List[$r]['Promo'].'</td>
        <td>'.round($List[$r]['IvaPercent']).'%</td>
        <td align="right">'.$List[$r]['Amount'].'</td>
		<td>'; 
		mn_field('button','ED','delArticle','','','','','del" onclick="
			document.InsertInvoice.action=\'_response/rsproxy.php?act=3&id='.$List[$r]['Ref'].'&pr=cdc_article\';
			subform(\'InsertInvoice\',\'invItems\');'); echo '</td>
		</tr>';
	  $Amount = $Amount+$List[$r]['Amount'];
	  } ?>
    </table>
</div>
	</td>
    </tr>
	<tr align="right" bgcolor="#F2F2F2">
	  <td></td>
	  <td colspan="3">Transporte: <?php 

	   $List = $GLOBALS['MYD']->db_array("SELECT 
					SUM(a.Amount) AS Amount
	  			FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON b.Ref = a.RefInvoice
				WHERE a.Office = {$_SESSION[$SESSOF]}
				AND b.TYPE = 9
				AND b.Status = 2
				AND b.TimeClose <= '{$GET[1]['TimeInvoice']}'
				GROUP BY b.Ref
				ORDER BY b.TimeClose DESC LIMIT 0,1");
		
	$Plafond = $GLOBALS['MYD']->db_array("SELECT 
					SUM(ccenter_petty.Plafond) AS Plaf
	  			FROM ccenter_petty
				WHERE Office = {$_SESSION[$SESSOF]}
				AND sysTime > {$GET[1]['TimeInvoice']} AND ('{$GET[1]['TimePay']}' OR '{$GET[1]['TimePay']}' = '')
				ORDER BY sysTime DESC
				LIMIT 0,1");
		
		if (!isset($Plafond[1])) {
		$Plaf = 0;
		} else {
		$Plaf = $Plafond[1]['Plaf'];
		}
		
		if (!isset($List[1])) {
		$Tranp = 0;
		} else {
		$Tranp = $List[1]['Amount'];
		} 
		echo 'EUR: ';
		echo $Plaf-$Tranp; ?></td>
    </tr>
	<tr align="right" bgcolor="#F2F2F2">
	  <td>&nbsp;</td>
	  <td colspan="3"><b>Total: EUR </b>
	  <?php mn_field('text','ED','Amount',$Amount,'20','14','','INPNUM'); ?></td>
    </tr>
	<tr align="right" bgcolor="#F2F2F2">
	  <td>&nbsp;</td>
    <td colspan="3">Saldo: EUR <?php echo $Plaf-$Amount;?>   
	</td>
    </tr>
</table>
 </form>